package com.xbongbong.paas.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSON;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.help.ProOssHelper;
import com.xbongbong.paas.model.ContractEditFileModel;
import com.xbongbong.paas.model.ContractTemplateBaseModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.model.impl.PaasFormModelImpl;
import com.xbongbong.paas.service.OnlineEditorService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.errorcodes.OnlineEditorErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.XbbCallErrorCodeEnum;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.pro.onlineEditor.ContractTemplateBaseListItemPojo;
import com.xbongbong.pro.onlineEditor.ContractTemplateBaseQueryItemPojo;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseDeleteDTO;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseEnableDTO;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseListDTO;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseQueryDTO;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseSaveDTO;
import com.xbongbong.pro.onlineEditor.dto.FileCopySaveDTO;
import com.xbongbong.pro.onlineEditor.dto.FileDetailDTO;
import com.xbongbong.pro.onlineEditor.dto.FileRenameDTO;
import com.xbongbong.pro.onlineEditor.sdk.dto.FiledUploadDTO;
import com.xbongbong.pro.onlineEditor.sdk.vo.FiledUploadVO;
import com.xbongbong.pro.onlineEditor.sdk.vo.GetFileDownloadUrlVO;
import com.xbongbong.pro.onlineEditor.sdk.vo.GetFileInfoVO;
import com.xbongbong.pro.onlineEditor.sdk.vo.ListUserInfoVO;
import com.xbongbong.pro.onlineEditor.vo.ContractTemplateBaseListVO;
import com.xbongbong.pro.onlineEditor.vo.ContractTemplateBaseQueryVO;
import com.xbongbong.pro.onlineEditor.vo.FileCopySaveVO;
import com.xbongbong.pro.onlineEditor.vo.FileDetailVO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.ContractEditFileEntity;
import com.xbongbong.saas.domain.entity.ContractTemplateBaseEntity;
import com.xbongbong.sys.model.UserModel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

/**
 * @author: yingjf
 * @date: 2023/3/8 14:11
 * @description: 在线编辑器
 */
@Slf4j
@AllArgsConstructor
@Service("onlineEditorService")
public class OnlineEditorServiceImpl implements OnlineEditorService {

    public static final Integer CONTRACT_TEMPLATE_BASE_MAX_COUNT = 100;

    private final ContractTemplateBaseModel contractTemplateBaseModel;
    private final ContractEditFileModel contractEditFileModel;
    private final ProOssHelper proOssHelper;
    private final PaasFormModel paasFormModel;
    private final UserModel userModel;


    @Override
    public GetFileInfoVO getFileInfo(String fileId) {

        ContractEditFileEntity contractEditFileEntity = contractEditFileModel.getLastByFileId(fileId);
        String fileName = contractEditFileEntity.getFileName();
        Integer fileVersion = contractEditFileEntity.getFileVersion();
        Long fileSize = contractEditFileEntity.getFileSize();
        Long addTime = contractEditFileEntity.getAddTime();
        Long updateTime = contractEditFileEntity.getUpdateTime();
        String creatorId = contractEditFileEntity.getCreatorId();


        GetFileInfoVO getFileInfoVo =
                new GetFileInfoVO(fileId, fileName, fileVersion, fileSize, addTime, updateTime,
                        creatorId, creatorId);


        return getFileInfoVo;
    }

    @Override
    public GetFileDownloadUrlVO getFileDownloadUrl(String fileId) {
        ContractEditFileEntity contractEditFileEntity = contractEditFileModel.getLastByFileId(fileId);
        String fileUrl = contractEditFileEntity.getFileUrl();
        GetFileDownloadUrlVO getFileDownloadUrlVO = new GetFileDownloadUrlVO(fileUrl);
        return getFileDownloadUrlVO;
    }

    @Override
    public FiledUploadVO filedUpload(String fileId, FiledUploadDTO filedUploadDTO) {
        try {
            Boolean is_manual = filedUploadDTO.getIs_manual();
            if (is_manual == null || !is_manual) {
                ContractEditFileEntity contractEditFileEntity = contractEditFileModel.getLastByFileId(fileId);
                Integer fileVersion = contractEditFileEntity.getFileVersion();
                FiledUploadVO filedUploadVO = new FiledUploadVO(fileId, contractEditFileEntity.getFileName(), fileVersion, contractEditFileEntity.getFileSize(), contractEditFileEntity.getAddTime()
                        , contractEditFileEntity.getAddTime(), "", "");
                return filedUploadVO;
            }

            String name = filedUploadDTO.getName();
            String[] split = name.split("\\.");
            Long size = filedUploadDTO.getSize();
            InputStream inputStream = filedUploadDTO.getFile().getInputStream();
            String fileUrl = proOssHelper.putObjectInputStream(inputStream, UUID.randomUUID().toString().replace("-", "")+"."+split[split.length - 1]);
            ContractEditFileEntity contractEditFileEntity = contractEditFileModel.getLastByFileId(fileId);
            String corpid = contractEditFileEntity.getCorpid();
            Integer fileVersion = contractEditFileEntity.getFileVersion();
            String creatorId = contractEditFileEntity.getCreatorId();



            ContractEditFileEntity newContractEditFileEntity = new ContractEditFileEntity();
            newContractEditFileEntity.setCorpid(corpid);
            newContractEditFileEntity.setFileId(fileId);
            newContractEditFileEntity.setFileVersion(fileVersion + 1);
            newContractEditFileEntity.setFileName(name);
            newContractEditFileEntity.setFileUrl(fileUrl);
            newContractEditFileEntity.setFileSize(size);
            newContractEditFileEntity.setCreatorId(creatorId);
            newContractEditFileEntity.setFileExt(split[split.length - 1]);
            contractEditFileModel.insert(newContractEditFileEntity);
            FiledUploadVO filedUploadVO = new FiledUploadVO(fileId, name, fileVersion + 1, size, newContractEditFileEntity.getAddTime()
                    , newContractEditFileEntity.getAddTime(), creatorId, creatorId);
            return filedUploadVO;

        } catch (Exception e) {
            log.error("com.xbongbong.paas.service.impl.OnlineEditorServiceImpl.filedUpload", e);
        }
        return null;
    }

    @Override
    public List<ListUserInfoVO> listUserInfo(List<String> userIds) {
        List<ListUserInfoVO> res = new ArrayList<>();
        if (CollectionsUtil.isNotEmpty(userIds)) {
            userIds.forEach(item -> {
                res.add(new ListUserInfoVO(item, " ", "avatar_url"));
            });
        }
        return res;
    }

    @Override
    public ContractTemplateBaseListVO contractTemplateBaseList(ContractTemplateBaseListDTO contractTemplateBaseListDTO) throws XbbException {
        ContractTemplateBaseListVO res = new ContractTemplateBaseListVO();
        String corpid = contractTemplateBaseListDTO.getCorpid();
        String templateName = contractTemplateBaseListDTO.getTemplateName();
        String fileName = contractTemplateBaseListDTO.getFileName();
        String createUserId = contractTemplateBaseListDTO.getCreateUserId();
        // 校验权限
        UserVO loginUser = contractTemplateBaseListDTO.getLoginUser();
        if (Objects.nonNull(loginUser)) {
            Set<String> permSet = loginUser.getPermSet();
            if (!permSet.contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_VIEW.getAlias())) {
                // 没有权限查看
                throw new XbbException(OnlineEditorErrorCodeEnum.APT_ERROR_340002);
            }
            if (permSet.contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_ADD.getAlias())) {
                // 新建
                res.setAllowAdd(1);
            }
            if (permSet.contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_ENABLE.getAlias())) {
                // 启用
                res.setAllowEnable(1);
            }
            if (permSet.contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_DELETE.getAlias())) {
                // 删除
                res.setAllowDelete(1);
            }
            if (permSet.contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_EDIT.getAlias())) {
                // 编辑
                res.setAllowEdit(1);
            }
        }

        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put(StringConstant.CORPID, corpid);
        params.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        if (StringUtil.isNotEmpty(templateName)) {
            params.put("templateNameLike", templateName);
        }
        if (StringUtil.isNotEmpty(fileName)) {
            params.put("fileNameLike", fileName);
        }
        if (StringUtil.isNotEmpty(createUserId)) {
            params.put("creatorId", createUserId);
        }
        params.put("orderByStr", "file_upload_time desc");
        List<ContractTemplateBaseEntity> contractTemplateBaseEntityList = contractTemplateBaseModel.findEntities(params);
        List<ContractTemplateBaseListItemPojo> contractTemplateBaseListItemPojos = packageListData(corpid, contractTemplateBaseEntityList);
        res.setData(contractTemplateBaseListItemPojos);
        return res;
    }

    @Override
    public void contractTemplateBaseEnable(ContractTemplateBaseEnableDTO contractTemplateBaseEnableDTO) throws XbbException {
        String corpid = contractTemplateBaseEnableDTO.getCorpid();
        Integer enable = contractTemplateBaseEnableDTO.getEnable();
        Long id = contractTemplateBaseEnableDTO.getId();
        // 校验权限
        UserVO loginUser = contractTemplateBaseEnableDTO.getLoginUser();
        if (Objects.nonNull(loginUser)) {
            if (!loginUser.getPermSet().contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_ENABLE.getAlias())) {
                // 没有权限启用
                throw new XbbException(OnlineEditorErrorCodeEnum.APT_ERROR_340003);
            }
        }
        ContractTemplateBaseEntity entity = new ContractTemplateBaseEntity();
        entity.setId(id);
        entity.setCorpid(corpid);
        entity.setEnable(enable);
        contractTemplateBaseModel.update(entity);
    }

    @Override
    public void contractTemplateBaseDelete(ContractTemplateBaseDeleteDTO contractTemplateBaseDeleteDTO) throws XbbException {
        String corpid = contractTemplateBaseDeleteDTO.getCorpid();
        Long id = contractTemplateBaseDeleteDTO.getId();
        // 校验权限
        UserVO loginUser = contractTemplateBaseDeleteDTO.getLoginUser();
        if (Objects.nonNull(loginUser)) {
            if (!loginUser.getPermSet().contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_DELETE.getAlias())) {
                // 没有权限删除
                throw new XbbException(OnlineEditorErrorCodeEnum.APT_ERROR_340004);
            }
        }
        ContractTemplateBaseEntity entity = new ContractTemplateBaseEntity();
        entity.setId(id);
        entity.setCorpid(corpid);
        entity.setDel(DelEnum.DELETE.getDel());
        contractTemplateBaseModel.update(entity);
    }

    @Override
    public void contractTemplateBaseSave(ContractTemplateBaseSaveDTO contractTemplateBaseSaveDTO) throws XbbException {
        String corpid = contractTemplateBaseSaveDTO.getCorpid();
        Long id = contractTemplateBaseSaveDTO.getId();
        String userId = contractTemplateBaseSaveDTO.getUserId();
        UserVO loginUser = contractTemplateBaseSaveDTO.getLoginUser();
        String templateName = contractTemplateBaseSaveDTO.getTemplateName();

        // 模板名称判重
        if (StringUtils.isNotEmpty(templateName)) {
            Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            params.put(StringConstant.CORPID, corpid);
            params.put("templateName", templateName);
            List<ContractTemplateBaseEntity> entities = contractTemplateBaseModel.findEntities(params);
            for (ContractTemplateBaseEntity item : entities) {
                Long itemId = item.getId();
                if ((id != null && itemId != id) || id == null) {
                    // 模板名称不可重复
                    throw new XbbException(OnlineEditorErrorCodeEnum.APT_ERROR_340001);
                }
            }
        }
        if (id == null) {
            if (loginUser != null && !loginUser.getPermSet().contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_ADD.getAlias())) {
                // 没有权限新建
                throw new XbbException(OnlineEditorErrorCodeEnum.APT_ERROR_340006);
            }
            Integer count = contractTemplateBaseModel.getCountByCorpid(corpid);
            if (count != null && count >= CONTRACT_TEMPLATE_BASE_MAX_COUNT) {
                // 创建模板不可超过100个，请先删除部分非常用模板后再新建
                throw new XbbException(OnlineEditorErrorCodeEnum.APT_ERROR_340007);
            }
            // 新建
            String fileName = contractTemplateBaseSaveDTO.getFileName();
            String fileUrl = contractTemplateBaseSaveDTO.getFileUrl();
            String fileExt = contractTemplateBaseSaveDTO.getFileExt();
            Long fileSize = contractTemplateBaseSaveDTO.getFileSize();
            ContractTemplateBaseEntity entity =
                    new ContractTemplateBaseEntity(corpid, templateName, fileName, fileUrl, fileExt, fileSize, userId, JSON.toJSONString(contractTemplateBaseSaveDTO.getViewPermission()));
            contractTemplateBaseModel.insert(entity);
        } else {
            // 更新
            if (loginUser != null && !loginUser.getPermSet().contains(ProPermissionAliasEnum.ONLINE_EDITOR_MANAGER_EDIT.getAlias())) {
                // 没有权限编辑
                throw new XbbException(OnlineEditorErrorCodeEnum.APT_ERROR_340005);
            }
            ContractTemplateBaseEntity oldContractTemplateBaseEntity = contractTemplateBaseModel.getByKey(id, corpid);
            ContractTemplateBaseEntity entity = new ContractTemplateBaseEntity();
            entity.setId(id);
            entity.setCorpid(corpid);
            if (templateName != null) {
                entity.setTemplateName(templateName);
            }
            if (contractTemplateBaseSaveDTO.getFileName() != null) {
                entity.setFileName(contractTemplateBaseSaveDTO.getFileName());
            }
            if (contractTemplateBaseSaveDTO.getFileUrl() != null) {
                entity.setFileUrl(contractTemplateBaseSaveDTO.getFileUrl());
            }
            if (contractTemplateBaseSaveDTO.getFileExt() != null) {
                entity.setFileExt(contractTemplateBaseSaveDTO.getFileExt());
            }
            if (contractTemplateBaseSaveDTO.getFileSize() != null) {
                entity.setFileSize(contractTemplateBaseSaveDTO.getFileSize());
            }
            if (!Objects.equals(oldContractTemplateBaseEntity.getFileUrl(), contractTemplateBaseSaveDTO.getFileUrl())) {
                entity.setFileUploadTime(DateTimeUtil.getInt());
            }
            if (contractTemplateBaseSaveDTO.getViewPermission() != null) {
                entity.setViewPermission(JSON.toJSONString(contractTemplateBaseSaveDTO.getViewPermission()));
            }
            entity.setEnable(contractTemplateBaseSaveDTO.getEnable());
            contractTemplateBaseModel.update(entity);
        }
    }

    @Override
    public ContractTemplateBaseQueryVO contractTemplateBaseQuery(ContractTemplateBaseQueryDTO contractTemplateBaseQueryDTO) throws XbbException {
        String corpid = contractTemplateBaseQueryDTO.getCorpid();
        Integer queryType = contractTemplateBaseQueryDTO.getQueryType();
        UserVO loginUser = contractTemplateBaseQueryDTO.getLoginUser();
        List<ContractTemplateBaseEntity> contractTemplateBaseEntityList = null;
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);

        if (queryType == 1 || queryType == 2 || queryType == 3){
            String templateNameLike = contractTemplateBaseQueryDTO.getTemplateName();
            if (StringUtil.isNotEmpty(templateNameLike)) {
                params.put("templateNameLike", templateNameLike);
            }
            String createUserId = contractTemplateBaseQueryDTO.getCreateUserId();
            if (StringUtil.isNotEmpty(createUserId)) {
                params.put("creatorId", createUserId);
            }
            Long startTime = contractTemplateBaseQueryDTO.getStartTime();
            Long endTime = contractTemplateBaseQueryDTO.getEndTime();
            if (startTime != null && endTime != null) {
                params.put("addTimeGte", startTime);
                params.put("addTimeLt", endTime);
            }
            params.put("enable", 1);
            params.put("del", 0);
        }else if (queryType == 5) {
            params.put("enable", 1);
            params.put("del", 0);
        }else if(queryType == 6){
            params.put("del", 0);
        }
        params.put("orderByStr", "file_upload_time desc");
        contractTemplateBaseEntityList = contractTemplateBaseModel.findEntities(params);
        if (queryType ==1 || queryType ==2 || queryType ==3 || queryType ==5) {
            // 权限校验过滤
            Iterator<ContractTemplateBaseEntity> it = contractTemplateBaseEntityList.iterator();
            while (it.hasNext()) {
                ContractTemplateBaseEntity contractTemplateBaseEntity = it.next();
                if (!validatePermission(contractTemplateBaseEntity, loginUser)) {
                    it.remove();
                }
            }
        }
        return packageQureyRes(corpid, contractTemplateBaseEntityList);
    }

    @Override
    public FileCopySaveVO fileCopySave(FileCopySaveDTO fileCopySaveDTO) throws XbbException {
        FileCopySaveVO fileCopySaveVO = new FileCopySaveVO();
        String corpid = fileCopySaveDTO.getCorpid();
        String fileName = fileCopySaveDTO.getFileName();
        String fileUrl = fileCopySaveDTO.getFileUrl();
        String fileExt = fileCopySaveDTO.getFileExt();
        Long fileSize = fileCopySaveDTO.getFileSize();
        String userId = fileCopySaveDTO.getUserId();
        ContractEditFileEntity entity = new ContractEditFileEntity(corpid, fileName, fileUrl, fileExt, fileSize, userId);
        entity.setFileId(UUID.randomUUID().toString().replace("-", ""));
        entity.setFileVersion(1);
        contractEditFileModel.insert(entity);
        String fileId = entity.getFileId();
        Integer fileVersion = entity.getFileVersion();
        fileCopySaveVO.setFileId(fileId);
        fileCopySaveVO.setFileVersion(fileVersion);
        return fileCopySaveVO;
    }

    @Override
    public void fileRename(FileRenameDTO fileRenameDTO) throws XbbException {
        String corpid = fileRenameDTO.getCorpid();
        String fileId = fileRenameDTO.getFileId();
        Integer fileVersion = fileRenameDTO.getFileVersion();
        String fileName = fileRenameDTO.getFileName();

        contractEditFileModel.updateFileNameByFileIdAndFileVersion(corpid, fileId, fileVersion, fileName);


    }

    @Override
    public FileDetailVO fileDetail(FileDetailDTO fileDetailDTO) throws XbbException {
        FileDetailVO fileDetailVO = new FileDetailVO();
        String corpid = fileDetailDTO.getCorpid();
        String fileId = fileDetailDTO.getFileId();
        Integer fileVersion = fileDetailDTO.getFileVersion();
        ContractEditFileEntity contractEditFileEntity = null;
        if (fileVersion == null) {
            contractEditFileEntity = contractEditFileModel.getLastByFileId(fileId);
        } else {
            contractEditFileEntity = contractEditFileModel.getOneByFileIdAndFileVersion(corpid, fileId, fileVersion);
        }
        fileDetailVO.setFileExt(contractEditFileEntity.getFileExt());
        fileDetailVO.setFileName(contractEditFileEntity.getFileName());
        fileDetailVO.setFileSize(contractEditFileEntity.getFileSize());
        fileDetailVO.setFileUrl(contractEditFileEntity.getFileUrl());
        fileDetailVO.setFileUrl(contractEditFileEntity.getFileUrl());
        fileDetailVO.setFileVersion(contractEditFileEntity.getFileVersion());
        return fileDetailVO;
    }

    /**
     * 封装查询结果
     * date: 2023/3/10 10:18
     *
     * @param corpid
     * @param contractTemplateBaseEntityList
     * @author yingjf
     */
    private ContractTemplateBaseQueryVO packageQureyRes(String corpid, List<ContractTemplateBaseEntity> contractTemplateBaseEntityList) throws XbbException {
        ContractTemplateBaseQueryVO contractTemplateBaseQueryVO = new ContractTemplateBaseQueryVO();
        if (CollectionsUtil.isEmpty(contractTemplateBaseEntityList)) {
            return new ContractTemplateBaseQueryVO();
        }
        List<ContractTemplateBaseQueryItemPojo> data = new ArrayList<>();
        contractTemplateBaseQueryVO.setData(data);
        List<String> userIdList = new ArrayList<>();
        contractTemplateBaseEntityList.forEach(item -> {
            String creatorId = item.getCreatorId();
            userIdList.add(creatorId);
        });
        Map<String, String> userNameMap = userModel.getUserNameMap(corpid, userIdList);

        contractTemplateBaseEntityList.forEach(item -> {
            ContractTemplateBaseQueryItemPojo pojo = new ContractTemplateBaseQueryItemPojo();
            pojo.setId(item.getId());
            pojo.setTemplateName(item.getTemplateName());
            pojo.setFileUrl(item.getFileUrl());
            pojo.setFileName(item.getFileName());
            pojo.setFileSize(item.getFileSize());
            pojo.setFileExt(item.getFileExt());
            pojo.setAddTime(item.getAddTime());
            pojo.setCreatorUserNmae(userNameMap.get(item.getCreatorId()));
            data.add(pojo);
        });
        return contractTemplateBaseQueryVO;
    }

    /**
     * 校验当前用户使用有使用表单的权限
     *
     * @param paasFormEntityExt
     * @param loginUser
     * @return false：无表单使用权限，true：有表单使用权限
     */
    private boolean validatePermission(ContractTemplateBaseEntity paasFormEntityExt, UserVO loginUser) {
        List<OptionalRangeEntity> permissionList = null;
        try {
            permissionList = JSON.parseArray(paasFormEntityExt.getViewPermission(),
                    OptionalRangeEntity.class);
        } catch (Exception e) {
            log.error("com.xbongbong.paas.service.impl.OnlineEditorServiceImpl.validatePermission", e);
        }
        return paasFormModel.getPermission(permissionList, loginUser);
    }

    /**
     * 封装合同模板库列表返回结果参数
     * date: 2023/3/8 14:42
     *
     * @param contractTemplateBaseEntityList
     * @author yingjf
     */
    private List<ContractTemplateBaseListItemPojo> packageListData(String corpid, List<ContractTemplateBaseEntity> contractTemplateBaseEntityList) throws XbbException {
        List<ContractTemplateBaseListItemPojo> res = new ArrayList<>();
        List<String> userIdList = new ArrayList<>();
        contractTemplateBaseEntityList.forEach(item -> {
            String creatorId = item.getCreatorId();
            userIdList.add(creatorId);
        });
        Map<String, String> userNameMap = userModel.getUserNameMap(corpid, userIdList);

        for (ContractTemplateBaseEntity item : contractTemplateBaseEntityList) {
            Long id = item.getId();
            String templateName = item.getTemplateName();
            String fileName = item.getFileName();
            String fileUrl = item.getFileUrl();
            Long fileSize = item.getFileSize();
            String fileExt = item.getFileExt();
            Long fileUploadTime = item.getFileUploadTime();
            Integer enable = item.getEnable();
            String creatorId = item.getCreatorId();
            // 处理权限
            String viewPermission = item.getViewPermission();
            String viewPermissionStr = null;
            List<OptionalRangeEntity> permissions = JSON.parseArray(viewPermission, OptionalRangeEntity.class);
            if (permissions == null || permissions.isEmpty()) {
                viewPermissionStr = I18nMessageUtil.getMessage(I18nStringConstant.NOT_LIMIT);
            } else {
                StringBuilder permission = new StringBuilder();
                for (OptionalRangeEntity optionalRangeEntity : permissions) {
                    if (StringUtil.isEmpty(permission.toString())) {
                        permission.append(optionalRangeEntity.getName());
                    } else {
                        permission.append("，" + optionalRangeEntity.getName());
                    }
                }
                viewPermissionStr = permission.toString();
            }
            ContractTemplateBaseListItemPojo contractTemplateBaseListItemPojo
                    = new ContractTemplateBaseListItemPojo(id, templateName, fileName, fileUrl, fileExt,
                    fileSize, fileUploadTime, permissions, viewPermissionStr, enable, userNameMap.get(creatorId));

            res.add(contractTemplateBaseListItemPojo);
        }
        return res;
    }


    public static void main(String[] args) {
        String text = "xxx";
        AES aes = SecureUtil.aes();
        // 加密并进行Base转码
        String encrypt = aes.encryptBase64(text);
        System.out.println(encrypt);

        // 解密为字符串
        String decrypt = aes.decryptStr(encrypt);
        System.out.println(decrypt);
    }


}
